package edu.gatech.oops.servlets;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CreateCourse, which stores a
 * newly created course in a database(but for now, a text file).
 */
public class CreateCourse extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateCourse() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		String CourseName = request.getParameter("name");
		String CourseNumber = request.getParameter("number");
		String CourseInstructor = request.getParameter("instructor");
		String CourseTime = request.getParameter("time");
		
		File file = new File("/home/pushkara/workspace/LabAssistan/Courses.txt");
		File cFile = new File("/home/pushkara/workspace/LabAssistan/" +CourseName +".txt");
		BufferedWriter cWriter = new BufferedWriter(new FileWriter(cFile, true));
		BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
		
		writer.write(CourseName + "," + CourseNumber + "," + CourseInstructor + "," + CourseTime + ","
				+"\n");
		writer.close();
		cWriter.close();
		response.sendRedirect("InstructorHome.jsp");	
		}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
